<?php


namespace App\Api\Fcjd;
use App\Domain\User\User as UserDomain;
use PhalApi\Api;
use PhalApi\Exception\BadRequestException;

/**
 * 非常检单md5
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Md5 extends Api
{

    public function getRules()
    {
        return array(
            'getMD5'       => array(),
        );
    }


    /**
     * getMD5
     */

    public function getMD5()
    {

        $AppList = \PhalApi\DI()->notorm->phone_app->where('state', 1)->fetchAll();

        // 初始化更新和未更新的数组
        $updatedList = array();
        $notUpdatedList = array();

        foreach ($AppList as $key => $value) {
            $url = $value['url'];
            $localFile = $value['name'] . '.js';

            $fileContents = file_get_contents($url); 
            if ($fileContents !== false) { 
                $md5 = md5($fileContents);
            }
            if ( $md5 != $value['md5']){
                \PhalApi\DI()->notorm->phone_app->where('id', $value['id'])->update(array('md5' => $md5));
                $updatedList[] = $value;
            }else{
                // 添加到未更新列表
                $notUpdatedList[] = $value;
            }
        }
        // 返回包含所有记录和更新状态的数组
        return array(
            'updated' => $updatedList,
            'not_updated' => $notUpdatedList
        );
    }
    
}